package com.oyjd.framework.ui.tab;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.oyjd.R;

/**
 * Tab菜单
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
@SuppressLint("ViewConstructor")
@SuppressWarnings("deprecation")
public class TabBar extends LinearLayout {
	private LayoutInflater flater;// 布局加载器
	private TabHost tabHost;
	private List<TabVO> data;

	/**
	 * 构造函数
	 * 
	 * @param context
	 */

	public TabBar(ActivityGroup context) {
		super(context);
		init();
	}

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param attrs
	 */
	public TabBar(ActivityGroup context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		// 初始化布局加载器
		flater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		flater.inflate(R.layout.inner_tabs, this, true);
		//
		tabHost = (TabHost) this.findViewById(R.id.tabhost);
		tabHost.setup(((ActivityGroup) getContext()).getLocalActivityManager());
	}

	/**
	 * 设置Tab项
	 * 
	 * @param data
	 *            数据
	 * @param startPos
	 *            起始位置,从集合下标0开始
	 */
	@SuppressWarnings("rawtypes")
	public void setTabData(final List<TabVO> data, int startPos) {
		this.data = data;
		if (startPos < 0 || startPos > data.size()) {
			startPos = 0;
		}
		for (int i = 0; i < data.size(); i++) {
			TabVO vo = data.get(i);
			TabHost.TabSpec spec = tabHost.newTabSpec(Integer.toString(i));
			spec.setIndicator(buildImaveView(i == startPos ? vo.tabPic2 : vo.tabPic1));
			if (vo.tabContentView instanceof View) {
				spec.setContent(tabContentFactory);
			} else if (vo.tabContentView instanceof Class) {
				spec.setContent(new Intent(getContext(), (Class) vo.tabContentView));
			}
			tabHost.addTab(spec);
		}
		tabHost.setOnTabChangedListener(onTabChangeListener);
		tabHost.setCurrentTab(startPos);
	}

	/**
	 * 点击Tab监听器
	 */
	private TabHost.OnTabChangeListener onTabChangeListener = new TabHost.OnTabChangeListener() {

		@Override
		public void onTabChanged(String tag) {
			int pos = Integer.parseInt(tag);
			for (int i = 0; i < data.size(); i++) {
				TabVO vo = data.get(i);
				ImageView imageView = (ImageView) tabHost.getTabWidget().getChildAt(i);
				imageView.setBackgroundResource(i == pos ? vo.tabPic2 : vo.tabPic1);
			}

		}
	};

	/**
	 * 构建tab图片
	 * 
	 * @param resid
	 *            图片资源id
	 * @return
	 */
	private ImageView buildImaveView(int resid) {
		ImageView iv = new ImageView(this.getContext());
		iv.setBackgroundResource(resid);
		return iv;
	}

	/**
	 * 获取对应Tab的内容视图
	 */
	private TabContentFactory tabContentFactory = new TabContentFactory() {

		@Override
		public View createTabContent(String tag) {
			int pos = Integer.parseInt(tag);
			View view = (View) data.get(pos).tabContentView;
			return view;
		}
	};

}
